$(function() {

	//获取url中的参数
	function getUrlParam(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
		var r = window.location.search.substr(1).match(reg); //匹配目标参数
		if (r != null) return unescape(r[2]);
		return null; //返回参数值
	}

	//退出登录
	$("#login_but").on("click", "#logout", function() {
		$.ajax({
			type: "GET",
			url: "http://127.0.0.1:8080/user/logout",
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					window.location.href = './index.html';
				}
			}
		});
	})

	//获取登陆用户session
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var node = $("#login_but");
				node.empty();
				var html = '<li style="color:#ffffff">' + data.data.userName + '，欢迎回来</li>';
				html += '<li>|</li><li><a href="javascript:void(0)" id="logout">退出</a></li>';
				node.html(html);
			}
		}
	});

	//进入商品详情列表
	$("#goods_list").on("click", ".gotoxq", function() {
		var id = $(this).attr("data-id");
		if (id == '') {
			layer.msg("数据异常");
		} else {
			var target = encodeURI("./xiangqing.html?id=" + id);
			location.href = target;
		}
	})

	//获取商品列表
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/goods/findAllGoods",
		dataType: "json",
		data: {
			"classify": getUrlParam('classify')
		},
		success: function(data) {
			if (data.code == 'success') {
				var html = "";
				var obj = data.data;
				obj.forEach(function(goods) {
					html += '<div class="mingxing fl mb20" style="border:2px solid #fff;' +
						'width:230px;cursor:pointer;" onmouseout="this.style.border=\'2px solid #fff\'" ' +
						'onmousemove="this.style.border=\'2px solid red\'"><div class="sub_mingxing">' +
						'<a href="javascript:void(0)" data-id="' + goods.id + '" class="gotoxq"><img src="' +
						goods.imgUrl + '" ' + 'alt=""></div><div class="pinpai">' + goods.goodsName +
						'</a></div><div class="jiage">' + goods.price + '元</div></div>';
				});
				$("#goods_list").html(html);
			}
		}
	});
})
